﻿using ChineseSchool.BLL;
using ChineseSchool.Models.Hethong;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ChineseSchool.Admin
{
    public partial class QL_Role : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
                return;
            BindDropdownRole();
            if (!string.IsNullOrEmpty(ddlRoles.SelectedValue))
                BindRepeaterUser(Convert.ToInt32(ddlRoles.SelectedValue));
        }

        private void BindDropdownRole()
        {
            var userTypes = new B_UserType();
            var lstRole = userTypes.GetAlls();
            ddlRoles.DataSource = lstRole;
            ddlRoles.Items.Clear();
            ddlRoles.DataTextField = "UserTypeName";
            ddlRoles.DataValueField = "UserTypeID";
            ddlRoles.DataBind();
        }

        private void BindRepeaterUser(int typeId)
        {
            var userServices = new B_User();
            var lstUsers = userServices.GetUserInGroup(Convert.ToInt32(typeId));
            grdUserRole.DataSource = lstUsers;
            grdUserRole.DataBind();
        }

        protected void DdlRolesSelectedIndexChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(ddlRoles.SelectedValue))
                BindRepeaterUser(Convert.ToInt32(ddlRoles.SelectedValue));
        }

        protected void BtnAddNewClick(object sender, EventArgs e)
        {
            var userName = txtUser.Text;
            var userServices = new B_User();

            if (userServices.CheckUserExist(userName) != 1)
                Response.Write("<script type='text/javascript'>alert('Không tồn tại user này trong hệ thống');</script>");

            if (!string.IsNullOrEmpty(ddlRoles.SelectedValue))
            {
                if (userServices.CheckUserGroup(userName, Convert.ToInt32(ddlRoles.SelectedValue)) == 1)
                    Response.Write("<script type='text/javascript'>alert('User " + userName + " đã thuộc group này');</script>");
            }

            var result = (new B_User()).AddGroupMember(userName, Convert.ToInt32(ddlRoles.SelectedValue));
            if (result)
            {
                Response.Write("<script type='text/javascript'>alert('Thêm mới thành công');</script>");
            }
            else
                Response.Write("<script type='text/javascript'>alert('Có lỗi xảy ra trong quá trình thao tác. Vui lòng thử lại');</script>");
        }

        protected void GrdUserRoleRowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                var result = (new B_User()).DeleteGroupMember(e.CommandArgument.ToString(), Convert.ToInt32(ddlRoles.SelectedValue));
                if (result)
                {
                    Response.Write("<script type='text/javascript'>alert('Xóa thành công');</script>");
                }
                else
                    Response.Write("<script type='text/javascript'>alert('Có lỗi xảy ra trong quá trình thao tác. Vui lòng thử lại');</script>");
            }
        }

        protected void GrdUserRoleRowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            if (!string.IsNullOrEmpty(ddlRoles.SelectedValue))
                BindRepeaterUser(Convert.ToInt32(ddlRoles.SelectedValue));
        }

        protected void GrdUserRolePageIndexChanging(object sender, GridViewPageEventArgs e)
        {

        }


    }
}
